package org.dentalcard.ui.schedule;

import org.dentalcard.entity.Shift;
import org.dentalcard.entity.WorkDay;
import org.dentalcard.service.PersistenceManager;
import org.dentalcard.service.impl.ScheduleDaoImpl;
import org.dentalcard.ui.api.tools.DecDialog;
import org.dentalcard.ui.main.MainFrameBuilder;
import org.dentalcard.ui.tools.DateTextField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @user: Vladimir Chernishov
 * @date: 21.12.11
 * @time: 11:38
 */
public class AddNewWorkDayPopup extends DecDialog {

    public AddNewWorkDayPopup(Frame owner) {
        super(owner, "Add New Work Day", true);
        setSize(new Dimension(300, 300));
        setResizable(true);
        center(owner);
        init();
    }

    private void init() {
        JPanel main = new JPanel(new GridBagLayout());
        int rowN = 0;
        main.add(new JLabel("Day"), new GridBagConstraints(0, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));

        final DateTextField date = new DateTextField("yyyy/MM/dd");
        main.add(date, new GridBagConstraints(1, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
        rowN++;
        main.add(new JLabel("Shift"), new GridBagConstraints(0, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));

        final JComboBox shift = new JComboBox(new ScheduleDaoImpl().getShiftList(MainFrameBuilder.getInstance().getDecUser()).toArray());
        main.add(shift, new GridBagConstraints(1, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
        rowN++;

        JButton ok = new JButton("OK");
        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    WorkDay workDay = new WorkDay();
                    workDay.setDay(date.getDate());
                    workDay.setShift((Shift)shift.getSelectedItem());
                    PersistenceManager.saveOrUpdate(workDay);
                } catch (Exception e1) {
                    e1.printStackTrace();
                    JOptionPane.showMessageDialog(AddNewWorkDayPopup.this, "Cannot save work day.");
                }
                dispose();
            }
        });
        main.add(ok, new GridBagConstraints(0, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));

        JButton cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        main.add(cancel, new GridBagConstraints(1, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));


        this.getContentPane().add(main);
    }
}
